//
//  ZYMainTabBarVc.swift
//  SwiftDemo1
//
//  Created by wzp on 2025/5/29.
//

import Foundation
import UIKit

struct ZYConfigItem: Codable {
    var clsName: String
    var title: String
    var imageName: String
}

struct ZYTabbarItem: IZYTabbarItem {
    var normalTitle: String
    var normalTitleColor: UIColor
    var normalImage: UIImage?
    
    var selectTitle: String?
    var selectTitleColor: UIColor?
    var selectImage: UIImage?
    
    var badgeValue: String?
}


class ZYMainTabBarVc: UITabBarController {
    private var customBar: ZYCustomTabbar = ZYCustomTabbar()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tabBar.isHidden = true
        
        addCustonTabbar()
        addChildVcs()
    }
    
    private func addChildVcs() {
        guard let bundleName = Bundle.main.infoDictionary?["CFBundleName"] as? String else {
            return
        }
        guard let filePath = Bundle.main.path(forResource: "config.json", ofType: nil),
        let data = NSData(contentsOfFile: filePath) as? Data else {
            return
        }
        
//        do {
//            let configs = try JSONDecoder().decode([ZYConfigItem].self, from: data)
//            for configItem in configs {
//                let fullName = "\(bundleName).\(configItem.clsName)"
//                if let vcClass = NSClassFromString(fullName) as? UIViewController.Type {
//                    let vc = vcClass.init()
//                    addChild(vc, configItem: configItem)
//                }
//            }
//            setupTabbar(configs: configs)
//        } catch  {
//            print("解析失败: \(error)")
//        }
        guard let configs = try? JSONDecoder().decode([ZYConfigItem].self, from: data) else {
            Logger.info("parse JSON failure， return")
            return
        }
        
        for configItem in configs {
            let fullName = "\(bundleName).\(configItem.clsName)"
            if let vcClass = NSClassFromString(fullName) as? UIViewController.Type {
                let vc = vcClass.init()
                addChild(vc, configItem: configItem)
            }
        }
        setupTabbar(configs: configs)
        
    }
    
    private func addChild(_ vc: UIViewController, configItem: ZYConfigItem) {
        let nvc = UINavigationController(rootViewController: vc)
        vc.title = configItem.title
        addChild(nvc)
    }
}

extension ZYMainTabBarVc: ZYCustomTabbarDelegate {
    
    private func addCustonTabbar() {
        view.addSubview(customBar)
        customBar.snp.makeConstraints { make in
            make.bottom.left.right.equalTo(0)
            make.height.equalTo(tabBarHeight())
        }
        customBar.delegate = self
        customBar.backgroundColor = .green
    }
    
    private func setupTabbar(configs: [ZYConfigItem]) {
        var tabbarItems = [ZYTabbarItem]()
        configs.forEach { config in
            var tabbarItem = ZYTabbarItem(normalTitle: config.title, normalTitleColor: .gray, normalImage:UIImage(named: config.imageName))
            tabbarItem.selectTitle = config.title
            tabbarItem.selectTitleColor = .orange
            tabbarItem.selectImage = UIImage(named: "\(config.imageName)_highlighted")
            tabbarItems.append(tabbarItem)
        }
        customBar.setupTabbar(tabbarItems)
    }
    
    func customTabbar(_ tabbar: ZYCustomTabbar, didSelectIndex index: Int) {
        self.selectedIndex = index
    }
}

